<?php
/**
* @package controller
* @file IndexController.php
* @date 08.09.2009
* @version 1.0
* @copyright sample
* @author Farzana
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class User_IndexController extends Cms_Controller_Action
{
	
	public function init()
	{
		
	}
	
    public function indexAction()
	{
		
		
	}

	public function viewAction()
	{
			
	}
	
	public function reportAction()
	{
			
	}
	
	public function viewActivityAction()
	{
		$id = $this->_getParam('id');
		$udata = Doctrine::getTable('User')->getUserDetailsEdit($id);
		$bcount = Doctrine::getTable('BigIdea')->getBigIdeaCountByUser($id);
		$ccount = Doctrine::getTable('Comment')->getCommentsCountByUser($id);
		$this->view->data = $udata;
		$this->view->bcount = $bcount;
		$this->view->ccount = $ccount;
		$this->_helper->layout->disableLayout();	
	}
		
	public function viewAllActivityAction()
	{
		$ucount = Doctrine::getTable('User')->getUserCount();
		$bcount = Doctrine::getTable('BigIdea')->getBigIdeaCountByUser();
		$ccount = Doctrine::getTable('Comment')->getCommentsCountByUser();
		$this->view->ucount = $ucount;
		$this->view->bcount = $bcount;
		$this->view->ccount = $ccount;
		$this->_helper->layout->disableLayout();	
	}
	/*
	 *DISPLAY GRID FOR SPECIFIC USER
	 */
	public function listAction()	
	{
		global $turnover;
		global $institutions,$currentYear,$major;
		$currentPage = isset($_POST['page'])? $_POST['page']:1;
		$resultsPerPage = isset($_POST['rp'])? $_POST['rp']:10;
		$quickSearchType = isset($_POST['qtype'])? $_POST['qtype']:'';
		$query = isset($_POST['query'])? $_POST['query']:'';
		$sortName = $_POST['sortname'];
		$sortOrder = $_POST['sortorder'];	  	
		$letterPressed=isset($_POST['letter_pressed'])? $_POST['letter_pressed'] :'';
		////////for get the details of manage sector grid///////////////////
		$pager=Doctrine::getTable('Organizations')->getUserGrid($currentPage,$resultsPerPage,$quickSearchType,$query,$sortName,$sortOrder,$letterPressed);
		$res = $pager->execute(array(1), Doctrine::HYDRATE_ARRAY);
		$total = $pager->getNumResults();
		
		$rows = NULL;
		foreach($res as $result) {
			$row =& $rows[];
			$row['id'] = $result['org_id'];
			$row['cell'][] = $result['org_id'];
			$row['cell'][] = $result['name_organisation'];
			$row['cell'][] = $result['respondents_name'];
			$row['cell'][] = $result['email_id'];
			$row['cell'][] = $result['respondent_designation'];
			if($result['telephone'] != 0)
			{
				$row['cell'][] = $result['telephone'];
			} else
			{
				$row['cell'][] = '-';
			}
			$row['cell'][] = $result['city'];
			$row['cell'][] = $result['country'];
			if($result['turnover'] == 0 || $result['turnover']=='') { 
				$row['cell'][] = '-';
			} else 
			{
				$row['cell'][] = $turnover[$result['turnover']];
			}
			$row['cell'][] = ($result['status']==1)?'Active':'De-Active';
			/*if($result['user_type']==1)
			{
				$row['cell'][] = 'User';
			} else if($result['user_type']==3)
			{
				$row['cell'][] = 'Moderator';
			} else
			{
				$row['cell'][] = 'Admin';
			}*/
			
		}
		$jsonData['page'] = $currentPage;
		$jsonData['total'] = $total;
		$jsonData['rows']  = $rows;
		$this->_helper->json->sendJson($jsonData);
		
	
	}
	 
	public function editAction()
	{
		$id = $this->_getParam('id');
		if($id!=0)
		{
			$contbid= Doctrine::getTable('User')->getUserDetails($id);
			$this->view->id=$contbid[0]['user_id'];
			$this->view->first_name=$contbid[0]['first_name'];
			$this->view->last_name=$contbid[0]['last_name'];
			$this->view->email_address = $contbid[0]['email_address'];
			$this->view->company_name = $contbid[0]['company_name'];
			$this->view->password = $contbid[0]['password'];
			$userTypes = Doctrine::getTable('UserType')->getUserTypes();
			for($i=0;$i<count($userTypes); $i++)
				{
					$userTypesList[$userTypes[$i]['id']] = $userTypes[$i]['user_type'];
				}
			$this->view->userTypes = $userTypesList;
			$this->view->uty = $contbid[0]['user_type'];
		}
	}
	public function saveProfileAction()
	{
		$update = Doctrine::getTable('User')->editUser($_POST);
		$this->_redirect('/user/');
	}
	public function activateAction()
	{
		$items = rtrim($_POST['items'],",");
		
		$result=Doctrine::getTable('Organizations')->activateUsers($items);
		$jsonData['total'] = $result;
		$this->_helper->json->sendJson($jsonData);
	}	
	public function deactivateAction()
	{
		$items = rtrim($_POST['items'],",");
		
		$result=Doctrine::getTable('Organizations')->deactivateUsers($items);
		$jsonData['total'] = $result;
		$this->_helper->json->sendJson($jsonData);
	}	
  	public function approveAction()
	{
		$items = rtrim($_POST['items'],",");
		
		$result=Doctrine::getTable('User')->approveUsers($items);
		$jsonData['total'] = $result;
		$this->_helper->json->sendJson($jsonData);
	}	
	public function disapproveAction()
	{
		$items = rtrim($_POST['items'],",");
		
		$result=Doctrine::getTable('User')->disapproveUsers($items);
		$jsonData['total'] = $result;
		$this->_helper->json->sendJson($jsonData);
	}	
	public function deleteAction()
	{
		$items = rtrim($_POST['items'],",");
		$result=Doctrine::getTable('User')->deleteUsers($items);
		
		$jsonData['total'] = count(explode(",", $items));
		$this->_helper->json->sendJson($jsonData);
	}
	
	public function contactUsersAction()
	{	
		//$id=$this->_getParam('id');
		$id = $_POST['userId'];
		$items = rtrim($id,",");
		$this->view->userIds = $items;
		$resultUser = Doctrine::getTable('User')->contactAllUsers($items);
		$this->view->resultUser = $resultUser; 
		$resultAdmin=Doctrine::getTable('User')->getAdminUser();
		$this->view->resultAdmin = $resultAdmin; 
		if($id==0)
		{
			$this->view->flag=0;
		}
		else
		{
			$this->view->flag=1;
		}
	}
		
	public function sendMailAction()
	{
		$this->_helper->layout->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$userIds = $_POST['userId'];
		$resultUser = Doctrine::getTable('User')->contactAllUsers($userIds);
		
		$mailFrom= explode('#',$_POST['mailFrom']);
		$fromName = $mailFrom[0];
		$fromEmail = $mailFrom[1];
		$subject = $_POST['mailSubject'];
		$body = $_POST['mailBody'];
		include('../lib/mail/SendEmail.php');

		foreach($resultUser as $arr)
		{
			$to = $arr['email'];
			$toName = $arr['first_name']." ".$arr['last_name'];
			sendMail($to,$subject,$body,$fromEmail,$fromName,$toName);
		}
		$this->_redirect('/admin-mvc/user/');	
	}

	public function excelAction()
	{
		$id = $_GET['id'];
		$this->_helper->layout->disableLayout();
		header("Content-Type: application/vnd.ms-excel");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header ("Content-Disposition: attachment; filename=user_report.xls" );
		$result = Doctrine::getTable('User')->getAllExport($id);
		//echo '<pre>';print_r($result);exit;
		//var_dump($result[0]);exit;
		$this->view->result = $result;
		
	}
	public function changeAction()
	{
		$userTypes = array(1=>'User',3=>'Moderator');
		$id = $this->_getParam('id');
		$this->_helper->layout->disableLayout();
		$user = Doctrine::getTable('User')->getUserType($id);
		$this->view->detail = $user;
		$this->view->types = $userTypes;
		$this->view->presentType = $user['user_type'];
		$this->view->userId = $id;
	
	}
	public function saveChangeAction()
	{
		//echo '<pre>';print_r($_GET);exit;
		$changeType = $_GET['changeType'];
		$userId = $_GET['userId'];
		$result = Doctrine::getTable('User')->updateType($changeType,$userId);
		$jsonData['total'] = $result;
		$this->_helper->json->sendJson($jsonData);
	}
	public function logoutAction()
	{
		session_destroy();
		$_SESSION['COR_ADMIN_ID']='';
		$this->_redirect('/admin-mvc/index/index');	
		
		
	}
}
